package com.yicong.ants.ui.scenic;

import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.cchao.simplelib.core.RxHelper;
import com.cchao.simplelib.ui.adapter.DataBindQuickAdapter;
import com.cchao.simplelib.ui.adapter.StatefulBindQuickAdapter;
import com.cchao.simplelib.ui.fragment.BaseStatefulFragment;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kennyc.view.MultiStateView;
import com.yicong.ants.BR;
import com.yicong.ants.R;
import com.yicong.ants.bean.scenic.ScenicItem;
import com.yicong.ants.databinding.CommonRecylerBinding;
import com.yicong.ants.manager.AdapterHelper;
import com.yicong.ants.manager.RouterHelper;
import com.yicong.ants.manager.http.RetrofitHelper;
import com.yicong.ants.utils.AmapHelper;

import java.util.HashMap;
import java.util.Map;

/**
 * 景区收藏
 *
 * @author cchao
 */
public class CollectionScenicFragment extends BaseStatefulFragment<CommonRecylerBinding> {

    private StatefulBindQuickAdapter<ScenicItem> mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.common_recyler;
    }

    @Override
    protected void initEventAndData() {
        initAdapter();
        onLoadData();
    }

    @Override
    protected void onLoadData() {
        if (AmapHelper.hasLocationInfo()) {
            loadPage(1);
        } else {
            showToast("请开启定位获取准确位置");
            loadPage(1);
        }
    }

    private void initAdapter() {
        mDataBind.recycler.setLayoutManager(new LinearLayoutManager(mContext));
        mAdapter = new StatefulBindQuickAdapter<ScenicItem>(R.layout.scenic_item_collect, null) {
            @Override
            public void loadPageData(int page) {
                loadPage(page);
            }

            @Override
            protected void convert(DataBindQuickAdapter.DataBindViewHolder helper, ScenicItem item) {
                helper.getBinding().setVariable(BR.bean, item);
                helper.setGone(R.id.level_label, false);
                helper.setGone(R.id.gold_distance, false);
            }
        };
        mAdapter.bindToRecyclerView(mDataBind.recycler);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                RouterHelper.toScenicDetail(getActivity(), mAdapter.getData().get(position).getScenic_id());
            }
        });
    }

    private void loadPage(final int page) {
        final Map<String, String> params = new HashMap<>();
        params.put("page", String.valueOf(page));

        if (page == 1) {
            mAdapter.setViewState(MultiStateView.VIEW_STATE_LOADING);
        }
        addSubscribe(RetrofitHelper.getApis().getCollectScenic(params)
            .compose(RxHelper.toMain())
            .subscribe(respBean -> {
                AdapterHelper.solvePageBean(mDataBind.recycler, page, respBean);
            }, RxHelper.getSwitchableErrorConsumer(mAdapter)));
    }
}
